<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" draggable overflow>
        <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-form-item label="供应商" prop="apply_name">
                {{apply_name}}
            </el-form-item>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="品名" prop="goods_name">
                        <el-input v-model="form.goods_name" placeholder="品名" readonly @click="checkGoods(1)"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="货号" prop="goods_code">
                        <el-input v-model="form.goods_code" placeholder="货号" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="设置样本" prop="sample_data">
                <el-button type="primary" size="small" icon="el-icon-plus" @click="checkSample">选择样本</el-button>
                <sc-form-table ref="table" v-model="form.sample_data" placeholder="暂无数据" hide-Add height="250">
                    <el-table-column label="样本名称" width="200">
                        <template #default="scope">
                            <div>名称: {{scope.row.title}}</div>
                            <div>编号: {{scope.row.code}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="颜色" prop="colors"></el-table-column>
                </sc-form-table>
            </el-form-item>
            <el-form-item label="设置花型" prop="flower_data">
                <sc-form-table ref="table" v-model="form.flower_data" :addTemplate="addTemplate" placeholder="暂无数据" height="250">
                    <el-table-column label="图片" align="center" width="200" prop="flower_image">
                        <template #default="scope">
                            <sc-upload width='50' height='50' v-model="scope.row.flower_image"></sc-upload>
                        </template>
                    </el-table-column>
                    <el-table-column label="花型" align="center" prop="flower">
                        <template #default="scope">
                            <el-input v-model="scope.row.flower" type="textarea" placeholder="花型" clearable></el-input>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
        <select-goods v-if="dialog.goods" ref="goodsDialog" @success="goodsBack" @closed="dialog.goods = false" :query="{is_show: 1}"></select-goods>
        <select-sample v-if="dialog.sample" ref="sampleDialog" @success="sampleBack" @closed="dialog.sample = false"></select-sample>
    </el-dialog>
</template>

<script>
import selectGoods from '@/views/basic/goods/selectGoods.vue';
import SelectSample from '../sample/selectSample.vue';

export default {
    components: { selectGoods, SelectSample },
    props: {
        query: {type: Object, default: () => ({})},
    },
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '设置品名',
                edit: '设置品名',
                show: '查看'
            },
            visible: false,
            isSaveing: false,
            apiObj: this.$API.basic.shopApply.list,
            selection: [],
            search: {
                keyword: null,
            },
            dialog: {
                goods: false,
                sample: false,
            },
            apply_name: "",
            form: {
                id: "",
                apply_id: "",
                goods_id: "",
                goods_name: "",
                goods_code: "",
                sample_data: [],
                flower_data: [],
            },
            rules: { 
                goods_name: [{ required: true, message: '请选择品名' }],
            },
            addTemplate: {},
        }
    },
    mounted() {
    },
    methods: {
        // 显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.basic.shopApply.edit.post(this.form);
					} else {
						res = await this.$API.basic.shopApply.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        // 获取供应商名称
        getName(data) {
            this.apply_name = data.apply_name
            this.form.apply_id = data.apply_id
        },
        //表单注入数据
		async setData(data) {
			this.form.id = data.id
			this.form.apply_id = data.apply_id
            let query = { apply_id: data.apply_id,goods_id: data.goods_id }
            var res = await this.$API.basic.shopApply.detail.get(query)
            this.form.goods_id = res.data.goods_id
            this.form.goods_name = res.data.goods_name
            this.form.goods_code = res.data.goods_code
            let array = []
            res.data.sample_data.map(item=>{
                array.push({
                    sample_id: item.sample.id,
					title: item.sample.title,
					code: item.sample.code,
					colors: item.sample.colors,
                })
            })
            this.form.sample_data = array
            this.form.flower_data = res.data.flower_data
		},
        // 选择事件
        selectionChange(selection) {
            this.selection = selection;
        },
        // 选中返回
        back() {
            this.$emit('success', this.selection, this.type)
            this.visible = false;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        checkGoods(e){
            this.dialog.goods = true
            this.$nextTick(() => {
                this.$refs.goodsDialog.open('add').setData(e)
            })
        },
        goodsBack(data){
            this.form.goods_id = data.id
            this.form.goods_name = data.name
            this.form.goods_code = data.code
        },
        checkSample(){
            this.dialog.sample = true
            this.$nextTick(() => {
                this.$refs.sampleDialog.open('add')
            })
        },
        sampleBack(data){
            let array = []
            data.map((item) => {
                let exist = this.common.isExist(item.id, this.form.sample_data, 'sample_id')
                if (exist) {
                    return
                }
				array.push({
					sample_id: item.id,
					title: item.title,
					code: item.code,
					colors: item.colors,
				})
			})
			this.form.sample_data = [...this.form.sample_data, ...array]
        },
    }
}
</script>

<style></style>
